package com.gfxy.modules.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.gfxy.aspect.DictTranslateService;
import com.gfxy.modules.base.service.ITeacherBaseInfoVoService;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.common.service.IGfxyCommonService;
import com.gfxy.modules.common.service.impl.CurrentDateService;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.entity.SysUser;
import com.gfxy.modules.system.service.ISysDepartService;
import com.gfxy.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.DictModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: lijixin 2021/10/30 12:18 下午
 */
@Api(tags="公共查询")
@RestController
@RequestMapping("/gfxy/common")
@Slf4j
public class GfxyCommonController {

    @Autowired
    private CurrentDateService currentDateService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDepartService departService;

    @Autowired
    private IGfxyCommonService gfxyCommonService;

    @Autowired
    private ITeacherBaseInfoVoService teacherBaseInfoService;

    @Autowired
    private DictTranslateService dictTranslateService;

    @AutoLog(value = "公共查询-查询当前考核时间")
    @ApiOperation(value="公共查询-查询当前考核时间", notes="公共查询-查询当前考核时间")
    @GetMapping(value = "/currentDate")
    public Result<?> getCurrentDate(
            HttpServletRequest req) {
        return Result.OK(currentDateService.getCurrentDate());
    }

    @AutoLog(value = "成员列表-通过UserName查询部门数据")
    @ApiOperation(value="成员列表-通过UserName查询部门数据", notes="成员列表-通过UserName查询部门数据")
    @GetMapping(value = "/queryDepartmentByUserName")
    public Result<?> queryDepartmentByUserName(@RequestParam(name="userName",required=true) String userName) {

            SysUser user = userService.getUserByName(userName);
            if(user == null){
                return Result.error("未找到用户数据");
            }
            List<SysDepart> sysDeparts = departService.queryUserTeachingDeparts(user.getId());

            if(sysDeparts==null || sysDeparts.size() ==0) {
                return Result.error("未找到部门数据");
            }
            if(sysDeparts.size()!=1){
                return Result.error("部门数据不唯一");
            }
            return Result.OK(sysDeparts.get(0));

    }

    @GetMapping(value = "/queryTeachingDepartment")
    public Result<List<DictModel>> queryTeachingDepartment(){
        Result<List<DictModel>> result = new Result<List<DictModel>>();
        List<DictModel> dictModelList = this.gfxyCommonService.queryTeachingDepartment();
        result.setSuccess(true);
        result.setResult(dictModelList);
        return result;
    }

    @GetMapping(value = "/queryTeacherBaseInfo")
    public Result<JSONObject> queryTeacherBaseInfo(@RequestParam(name="username",required=true) String username){

        TeacherBaseInfoVo teacherBaseInfo = teacherBaseInfoService.queryTeacherBaseInfoVoByTeacherId(username);
        JSONObject teacherBaseInfoObj = dictTranslateService.translateDictForOne(teacherBaseInfo);
        return Result.OK(teacherBaseInfoObj);
    }
}
